﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Micrhard.FMCG.Server.DataAccess;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Micrhard.FMCG.Server.Msg;
using Micrhard.FMCG.Server.DTO;
using Micrhard.FMCG.Server.Models;

namespace Micrhard.FMCG.Server.Impl
{
    public class IspCategoryImpl : IIspCategory
    {
        public bool IsNameDuplicate(Models.IspCategoryModel model)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var cnt = db.CheckListTemplateCategories.Count(c => c.CheckListTemplate.CheckListTemplateID == model.TemplateID
                && c.Name == model.Name);
            return cnt > 0 ? true : false;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(Models.IspCategoryModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var op = model.ToEntity();     
            try
            {            
                op.CreateTime = DateTime.Now;            
                db.CheckListTemplateCategories.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, IspCategoryMsg.AddFailed);
                Log.Error("IIspCategory.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.IspCategoryModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.CheckListTemplateCategories.SingleOrDefault(l => l.CheckListCategoryID== model.CheckListCategoryID);
            if (entity != null)
            {
                entity.Name = model.Name;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, IspCategoryMsg.UpdateFailed);
                Log.Error("IIspCategory.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.CheckListTemplateCategories.Where(l => ids.Contains(l.CheckListCategoryID));
            foreach (var e in entity)
            {
                //模板是否在使用
                if (e.CheckListTemplateItems.Count == 0)
                {
                    db.CheckListTemplateCategories.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(IspCategoryMsg.NameIsInUsing, e.Name));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, IspCategoryMsg.DeleteFailed);
                    Log.Error("IIspCategory.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.IspCategoryModel model)
        {
            throw new NotImplementedException();
        }

        public Models.IspCategoryModel GetModelByID(Guid id)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var mcmp = db.CheckListTemplateCategories.SingleOrDefault(c => c.CheckListCategoryID == id);
            if (mcmp != null)
            {
                return mcmp.ToModel();
            }
            return null;
        }

        public Models.IspCategoryModel GetModelByModel(Models.IspCategoryModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.IspCategoryModel> Query(Models.IspCategoryModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var cmp = db.Employees.SingleOrDefault(e => e.UserName == this.scope.CurrentUserName);
            IEnumerable<CheckListTemplateCategory> entities = from b in db.CheckListTemplateCategories
                                                              where  b.CheckListTemplate.CompanyID == cmp.CompanyID
                                                              && b.TemplateID == Model.TemplateID
                                                              select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
            {
                 List<Models.IspCategoryModel> list= pagedEntities.Select(m => m.ToModel()).ToList();
                foreach(Models.IspCategoryModel l in list)
                {
                    var t= db.CheckListTemplates.SingleOrDefault(n=>n.CheckListTemplateID==l.TemplateID);
                    l.TemplateName = t.TemplateName;
                }
                return list;
            }
            return new List<IspCategoryModel>();
        }

        public List<Models.IspCategoryModel> Query(Models.IspCategoryModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

       public  List<IspCategoryModel> GetAllCategory()
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            IEnumerable<CheckListTemplateCategory> entities = from b in db.CheckListTemplateCategories
                                                          select b;
            if (entities.Count() > 0)
            {
                return entities.Select(m => m.ToModel()).ToList();
            }

            return new List<IspCategoryModel>();
        }
    }
}
